﻿using System.Collections;

namespace Function.HotUpdate
{
    public enum HotState
    {
        None,
        LightHouse,
        CheckNeedUpdate,
        InternalId,
        UpdateAddressable,
        Download,
        SaveVersion,
    }

    public abstract class HotStep
    {
        protected float _restTime = 0;
        protected bool _cb = false; //Step失败后否继续Step
        protected bool _success = false; //Step是否成功
        protected HotContext _hotContext;
        protected HotStep _nextStep;
        public abstract HotState GetHotState();

        public void SetSuccess(bool success)
        {
            _success = success;
        }

        protected abstract IEnumerator OnExecute();

        public HotStep(HotContext hotContext, bool cb)
        {
            _hotContext = hotContext;
            _cb = cb;
        }

        public HotStep Next(HotStep nextStep)
        {
            _nextStep = nextStep;
            return _nextStep;
        }

        public IEnumerator Execute()
        {
            _hotContext._step = this;
            yield return OnExecute();
        }

        public IEnumerator Finish()
        {
            if (_nextStep != null)
            {
                if (_success)
                    yield return _nextStep.Execute();
                else if (_cb)
                    yield return _nextStep.Execute();
            }
            else
            {
                HotManager.Instance._finishUpdate?.Invoke(); //所有Step做完代表热更新完成
            }
        }

        public virtual void StopNet() //断网
        {
            _hotContext.Info("断网", "请检查网络连接");
        }

        public virtual void ResNet() //网络重新连接
        {
            _hotContext.Info("重新连接", "重新连接啦.....");
        }
    }
}